package com.tanhua.server.controller;

import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.vo.CountsVO;
import com.tanhua.model.vo.FriendVO;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.UserInfoVO;
import com.tanhua.server.service.UserInfoService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Xiao
 * @date 2021/10/22
 */
@RestController
@RequestMapping("/users")
public class UsersController {
    @Autowired
    private UserInfoService userInfoService;

    /**
     * 用户资料-读取
     * @param token
     * @param userID
     * @return
     */
    @GetMapping
    public ResponseEntity users(
            @RequestHeader("Authorization") String token,
            Long userID) {
        UserInfoVO userInfoVO = userInfoService.users(token, userID);
        /*if (userInfoVO == null) {
            return ResponseEntity.status(401).body(null);
        }*/
        return ResponseEntity.ok(userInfoVO);
    }

    /**
     * 用户资料-保存(更新)
     * @param token
     * @param userInfo
     * @return
     */
    @PutMapping
    public ResponseEntity updateUserInfo(
            @RequestHeader("Authorization") String token,
            @RequestBody UserInfo userInfo) {
        boolean result = userInfoService.updateUserInfo(userInfo, token);
        /*if (!result) {
            return ResponseEntity.status(401).body(null);
        }*/
        return ResponseEntity.ok("修改成功！");
    }

    /**
     * 用户资料-更新头像
     * @param headPhoto
     * @return
     * @throws IOException
     */
    @PostMapping("/header")
    public ResponseEntity header(MultipartFile headPhoto) throws IOException {
        userInfoService.updateHead(headPhoto);
        return ResponseEntity.ok("头像上传成功！");
    }

    /**
     * 我的-互相喜欢、喜欢、粉丝、谁看过我 - 翻页列表
     * @param type     1-互相关注、2-我关注、3-粉丝、4-谁看过我
     * @param page
     * @param pagesize
     * @return
     */
    @GetMapping("/friends/{type}")
    public ResponseEntity queryFriends(
            @PathVariable String type,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pagesize,
            @RequestParam(required = false) String nickname) {
        if (StringUtils.isNotBlank(nickname)) {
            page = 1;
        }
        PageResult pageResult = userInfoService.queryFriends(type, page, pagesize, nickname);
        return ResponseEntity.ok(pageResult);
    }

    /**
     * 我的-粉丝-喜欢
     * @param uid
     * @return
     */
    @PostMapping("/fans/{uid}")
    public ResponseEntity fans(@PathVariable Long uid) {
        userInfoService.fans(uid);
        return ResponseEntity.ok("已喜欢！");
    }

    /**
     * 我的-喜欢-取消喜欢
     * @param uid
     * @return
     */
    @DeleteMapping("/like/{uid}")
    public ResponseEntity removeLove(@PathVariable Long uid) {
        userInfoService.removeLove(uid);
        return ResponseEntity.ok("已取消喜欢！");
    }

    /**
     * 我的-互相喜欢，喜欢，粉丝 - 统计
     * @return
     */
    @GetMapping("/counts")
    public ResponseEntity getCounts() {
        CountsVO countsVO = userInfoService.getCounts();
        return ResponseEntity.ok(countsVO);
    }

    /**
     * 查看是否已喜欢
     * @param uid
     * @return
     */
    @GetMapping("/{uid}/alreadyLove")
    public ResponseEntity alreadyLove(@PathVariable Long uid) {
        return ResponseEntity.ok(new FriendVO());
    }
}
